﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace GrayParrot.Web.Controls
{
    [Serializable]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Ordering : Feature<bool>
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)]
        public Orders<OrderingComparer<Order>> Orders { get; set; }

        public bool OrderCellsTop { get; set; }
        public bool OrderClasses { get; set; }
        public bool OrderMulti { get; set; }

        public Ordering()
        {
            this.OrderCellsTop = false;
            this.OrderClasses = true;
            this.OrderMulti = true;
        }

        public override string ToString()
        {
            string retValue = string.Empty;

            retValue += string.Format("\"ordering\": {0}\n", this.Value.ToString().ToLower());

            if (!this.OrderClasses)
            {
                //if (!string.IsNullOrWhiteSpace(retValue))
                //{
                //    retValue += ",";
                //}

                retValue += ",\"orderClasses\": false\n";
            }

            if (!this.OrderMulti)
            {
                retValue += ",\"orderMulti\": false\n";
            }

            if (this.OrderCellsTop)
            {
                retValue += ",\"orderCellsTop\": true\n";
            }

            string m_oders = "[";
            for (int i = 0; i < this.Orders.Count(); i++)
            {
                m_oders += string.Format("[{0}, '{1}']", Orders[i].ColumnIndex, (Orders[i].Direction == OrderDirections.asc ? "asc" : "desc"));

                if (i < this.Orders.Count())
                {
                    m_oders += ", ";
                }
            }
            m_oders += "]";

            retValue += string.Format("\"order\": {0}", m_oders);

            return retValue;
        }
    }
}
